<template>
  <!-- 接单-->
  <a-modal :width="500" v-model:visible="showReceviceDialog" title="接单" title-align="start" :mask-closable="false" @cancel="closedDialog" :on-before-ok="submitAudit">
    <a-radio-group v-model="state">
      <a-radio :value="true">确认接单</a-radio>
      <a-radio :value="false">拒绝接单</a-radio>
    </a-radio-group>
    <div style="height: 200px; margin-top: 20px">
      <div v-show="state" class="flex-c">
        <p>请选择采购员：</p>
        <a-select v-model="user" value-key="id" placeholder="请选择成员" allow-search allow-clear style="width: 300px">
          <a-option v-for="(item, index) in useBaseData.userList" :key="index" :value="item">{{ item.userName }}</a-option>
        </a-select>
      </div>
      <div v-show="!state">
        <p style="margin-bottom: 10px; color: #999">
          <span class="warningColor">*</span>
          <span>注：拒绝必须填写拒绝原因</span>
        </p>
        <a-textarea v-model="remark" style="height: 100px" placeholder="请填写驳回原因" allow-clear />
      </div>
    </div>
  </a-modal>
</template>
<script lang="ts" setup>
  import { ref, onMounted } from 'vue'
  import { Message } from '@arco-design/web-vue'
  import useBaseDataStore from '@/store/modules/base-data'
  import { receivePurchasePlan } from '@/api/purchaseApplication'

  onMounted(() => {
    useBaseData.getUserList()
  })
  const emit = defineEmits(['closed', 'success'])
  const props = defineProps({
    isShow: {
      type: Boolean,
      default: false,
    },
    ids: {
      type: Array,
      default: () => [],
    },
  })

  const useBaseData = useBaseDataStore()
  const showReceviceDialog = ref(props.isShow)
  const state = ref(true)
  const remark = ref()
  const user = ref<any>()

  // 确认
  async function submitAudit() {
    if (!state.value && !remark.value) {
      Message.error('必须填写拒绝接单原因')
      return false
    }
    if (state.value && !user.value?.id) {
      Message.error('必须选择采购员')
      return false
    }
    let _params: any = {
      ids: props.ids,
      pass: state.value,
    }
    if (state.value) {
      _params.purchaseUname = user.value?.userName
      _params.purchaseUid = user.value?.id
    } else {
      _params.remark = remark.value
    }
    const res: any = await receivePurchasePlan(_params)
    if (!res) return false
    Message.success(state.value ? '接单成功' : '已拒绝接单')
    emit('success')
    emit('closed')
    return true
  }
  // 关闭
  function closedDialog() {
    showReceviceDialog.value = false
    emit('closed')
  }
</script>
<style lang="less" scoped></style>
